<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
            <el-form-item label="方案名称" prop="planName">
                <el-input v-model="queryParams.planName" placeholder="请输入方案名称" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="车型名称" prop="modelName">
                <el-input v-model="queryParams.modelName" placeholder="请输入车型名称" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="是否启用" prop="isAvailable">
                <el-select v-model="queryParams.isAvailable" @1change="handleQuery">
                    <el-option label="启用" :value="1"></el-option>
                    <el-option label="停用" :value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['plan:luxuryPlan:add']">新增</el-button>
            </el-col>
            <!--  <el-col :span="1.5">
                <el-button type="danger"
                           plain
                           icon="el-icon-delete"
                           size="mini"
                           :disabled="multiple"
                           @click="handleDelete"
                           v-hasPermi="['plan:plan:remove']">删除</el-button>
            </el-col> -->
            <!-- <el-col :span="1.5">
                <el-button type="warning"
                           plain
                           icon="el-icon-download"
                           size="mini"
                           :loading="exportLoading"
                           @click="handleExport"
                           v-hasPermi="['plan:plan:export']">导出</el-button>
            </el-col> -->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
            <!-- <el-table-column label="方案id" align="center" prop="planId" /> -->
            <el-table-column label="方案名称" align="center" prop="planName" />
            <el-table-column label="车型名称" align="center" prop="modelInfo" />
            <el-table-column label="创建时间" align="center" prop="updateTime" width="180">
                <!-- <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
                </template> -->
            </el-table-column>
            <el-table-column label="所属门店" align="center" prop="deptName" />
            <el-table-column label="所属租户" align="center" prop="tenantName" />
            <el-table-column label="是否启用" align="center">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_available" :value="scope.row.isAvailable" />
                </template>
            </el-table-column>
            <el-table-column label="操作" align="left" class-name="small-padding fixed-width" fixed="right" width="180">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-search" @click="handleDetail(scope.row)" v-hasPermi="['plan:plan:edit']">详情</el-button>
                    <el-button v-if="scope.row.isAvailable===0" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['plan:plan:edit']">修改</el-button>
                    <!-- <el-button  v-if="scope.row.isAvailable===0" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['plan:plan:remove']">删除</el-button> -->
                    <el-button v-if="scope.row.isAvailable===0" size="mini" type="text" icon="el-icon-switch-button" @click="handleAvailable(scope.row)" v-hasPermi="['car:model:edit']">启用</el-button>
                    <el-button v-if="scope.row.isAvailable===1" size="mini" type="text" icon="el-icon-switch-button" @click="handleAvailable(scope.row)" v-hasPermi="['car:model:edit']">停用</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改方案主对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="1160px" destroy-on-close append-to-body @close="nowPlanId=false">
            <MmvAdd :form="form" :planId="nowPlanId" ref="mmvAdd" :key="open"></MmvAdd>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm" :loading="addPlanLoading">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <el-dialog title="详情" :visible.sync="openDetail" width="1160px" destroy-on-close append-to-body @close="nowPlanId=false">
            <MmvDetail :form="form" :planId="nowPlanId" ref="mmvDetail" :key="openDetail"></MmvDetail>
            <div slot="footer" class="dialog-footer">
                <el-button @click="openDetail=false">关闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import MmvAdd from './add.vue'
import MmvDetail from './detail.vue'
import { listPlan, getPlan, delPlan, addPlan, updatePlan, exportPlan, stopUsingEnabled } from "@/api/plan/luxuryPlan";
import cloneDeep from 'lodash/cloneDeep'

const form = {
    "planId": "",
    "planName": "",
    "modelId": "",
    "modelName": "",
    "modelInfo": "",
    "configurationId": "",
    "isSetMeal": 2,
    "standardVo": {
        "mondayPrice": 0,
        "tuesdayPrice": 0,
        "wednesdayPrice": 0,
        "thursdayPrice": 0,
        "fridayPrice": 0,
        "saturdayPrice": 0,
        "sundayPrice": 0
    },
    "holidayVo": [
        // {
        //     "holidayType": 1,
        //     "holidayId": "H1646382576029",
        //     "holidayName": "国庆",
        //     "startTime": "2022-10-01",
        //     "endTime": "2022-10-07",
        //     "price": 90.00
        // },
        // {
        //     "holidayType": 2,
        //     "holidayId": "",
        //     "holidayName": "特殊日期",
        //     "startTime": "2022-10-01",
        //     "endTime": "2022-10-03",
        //     "price": 85.00
        // }
    ],
    "mealVo": [
        // {
        //     "mealId": "M1646387553951",
        //     "mealName": "5天套餐",
        //     "discount": 0.98
        // }
    ],
    "chargeItemsVo": [
        // {
        //     "chargeItemsName": "儿童座椅",
        //     "chargeItemsType": 1,
        //     "uniquenessCode": "RTZY",
        //     "price": 100.00
        // }
    ]
}

export default {
    name: "Plan",
    dicts: ['store_type', 'sys_available', 'store_class', 'service_mode'],
    components: {
        MmvAdd,
        MmvDetail,
    },
    data() {
        return {
            openDetail: false,
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 方案主表格数据
            planList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            addPlanLoading: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                planName: null,
                modelId: null,
                modelName: null,
                configurationId: null,
                isAllMember: null,
                memberNo: null,
                isSetMeal: null,
                isAvailable: null,
                deptId: null,
                tenantId: null
            },
            // 表单参数
            form: cloneDeep(form),
            // 表单校验
            rules: {
            },
            nowPlanId: null,
        };
    },
    created() {
        this.getList();
    },
    methods: {
        handleAvailable(row) {
            const id = row.planId;
            var tip = '停用';
            var isAvailable = 0;

            if (row.isAvailable == 0) {
                tip = '启用';
                var isAvailable = 1;
            }

            this.$modal.confirm('是否确认' + tip + '方案【' + row.planName + '】？').then(function () {
                var data = {
                    planId: id,
                    isAvailable: isAvailable
                };
                return stopUsingEnabled(data);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("操作成功");
            }).catch(() => { });
        },
        /** 查询方案主列表 */
        getList() {
            this.loading = true;
            listPlan(this.queryParams).then(response => {
                this.planList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.addPlanLoading = false;
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = cloneDeep(form);
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.planId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加方案";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.nowPlanId = row.planId;
            this.open = true;
            this.title = '修改方案';
            // this.reset();
            // const planId = row.planId || this.ids
            // getPlan(planId).then(response => {
            //     this.form = response.data.data;
            //     this.open = true;
            //     this.title = "修改方案";
            // });
        },
        /** 详情按钮操作 */
        handleDetail(row) {
            this.nowPlanId = row.planId;
            this.openDetail = true;
        },
        /** 提交按钮 */
        submitForm() {
            const form = this.$refs['mmvAdd'].submitForm();
            this.form = cloneDeep(form);
            // return;
            // this.$refs["form"].validate(valid => {
            //     if (valid) {
            if (this.form.planId) {
                this.addPlanLoading = true;
                updatePlan(this.form).then(response => {
                    this.addPlanLoading = false;
                    this.$modal.msgSuccess("修改成功");
                    this.open = false;
                    this.getList();
                }).catch(err => {
                    this.addPlanLoading = false;
                });

            } else {
                this.addPlanLoading = true;
                addPlan(this.form).then(response => {
                    this.addPlanLoading = false;
                    this.$modal.msgSuccess("新增成功");
                    this.open = false;
                    this.getList();
                }).catch(err => {
                    this.addPlanLoading = false;
                });
            }
            //     }
            // });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const planIds = row.planId || this.ids;
            this.$modal.confirm('是否确认删除数据项？').then(function () {
                return delPlan(planIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$modal.confirm('是否确认导出所有方案主数据项？').then(() => {
                this.exportLoading = true;
                return exportPlan(queryParams);
            }).then(response => {
                this.$download.name(response.msg);
                this.exportLoading = false;
            }).catch(() => { });
        }
    }
};
</script>
